package com.isoft.c2team3service2.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.isoft.c2team3service2.dto.PostDTO;
import com.isoft.c2team3service2.entity.Post;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 帖子数据访问接口
 */
@Mapper
public interface PostMapper extends BaseMapper<Post> {

    /**
     * 更新浏览量
     */
    @Update("UPDATE post SET view_count = view_count + 1 WHERE id = #{postId}")
    int updateViewCount(@Param("postId") Integer postId);

    @Select("SELECT p.*, h.photo AS photourll FROM post p " +
            "JOIN tb_head h ON p.user_id = h.user_id " +
            "WHERE p.status = 1 AND p.is_private = 0 " +
            "ORDER BY p.view_count DESC, p.like_count DESC " +
            "LIMIT #{limit}")
    List<PostDTO> findHotPosts(@Param("limit") Integer limit);

    @Select("SELECT p.*, h.photo AS photourll FROM post p " +
            "JOIN tb_head h ON p.user_id = h.user_id " +
            "WHERE p.status = 1 AND p.is_private = 0 " +
            "ORDER BY p.create_time DESC " +
            "LIMIT #{limit}")
    List<PostDTO> findLatestPosts(@Param("limit") Integer limit);

    @Select("SELECT p.*, h.photo AS photourll FROM post p " +
            "JOIN tb_head h ON p.user_id = h.user_id " +
            "WHERE p.status = 1 AND p.user_id = #{id} " +
            "ORDER BY p.create_time DESC ")
    List<PostDTO> findByUserId(@Param(("id")) Integer userId);

    @Select("SELECT p.*, h.photo FROM post AS p " +
            "JOIN tb_head AS h ON  h.user_id = p.user_id " +
            "WHERE p.status = 1 AND p.is_private = 0 AND p.artist_name = #{artist_name} " +
            "ORDER BY p.create_time DESC " +
            "LIMIT #{limit}  ")
    List<PostDTO> findPosts(@Param("artist_name") String artistName, @Param("limit") Integer limit);

//    @Select("SELECT p.*, h.photo FROM post AS p " +
//            "JOIN tb_head AS h ON  h.user_id = p.user_id " +
//            "WHERE p.status = 1 AND p.is_private = 0 AND p.user_id = #{userId} " +
//            "ORDER BY p.create_time DESC " +
//            "LIMIT 10  ")
//    List<PostDTO> vagueSearch(@Param("userId") Integer userId);

}
